# -*- coding:UTF-8 -*-
'''
Created on 2009. 10. 23.

@author: cwjeon
reference : http://code.google.com/intl/ko-KR/apis/calendar/data/1.0/developers_guide_python.html
my wiki: http://cwjeon.pbworks.com/goolge_calendar
'''

import gdata.docs.service
try:
    from xml.etree import ElementTree # for Python 2.5 users
except ImportError:
    from elementtree import ElementTree
import gdata.calendar.service
import gdata.service
import atom.service
import gdata.calendar
import atom
import getopt
import sys
import string
import time

def get_my_docs():
    # Create a client class which will make HTTP requests with Google Docs server.
    client = gdata.docs.service.DocsService()
    # Authenticate using your Google Docs email address and password.
    client.ClientLogin('test', '1234')

    # Query the server for an Atom feed containing a list of your documents.
    documents_feed = client.GetDocumentListFeed()
    # Loop through the feed and extract each document entry.
    for document_entry in documents_feed.entry:
      # Display the title of the document on the command line.
      print document_entry.title.text

def GetAuthSubUrl():
    next = 'http://www.coolcalendarsite.com/welcome.pyc'
    scope = 'http://www.google.com/calendar/feeds/'
    secure = False
    session = True
    calendar_service = gdata.calendar.service.CalendarService()
    return calendar_service.GenerateAuthSubURL(next, scope, secure, session);

def PrintUserCalendars(calendar_service):
    feed = calendar_service.GetAllCalendarsFeed()
    print feed.title.text
    for i, a_calendar in enumerate(feed.entry):
        print '\t%s. %s' % (i, a_calendar.title.text,)

class CalendarEventMsg(object):
    def __init__(self, title, content, where='', start_time=None, end_time=None):
        self.title = title
        self.content = content
        self.where = where
        self.start_time = start_time
        self.end_time = end_time

def create_event(calendar_service, cem):
    event = gdata.calendar.CalendarEventEntry()
    event.title = atom.Title(text=cem.title)
    event.content = atom.Content(text=cem.content)
    event.where.append(gdata.calendar.Where(value_string=cem.where))
    reminder = gdata.calendar.Reminder(minutes='10')
    reminder._attributes['method'] = 'method'
    reminder.method = 'sms'

    if cem.start_time is None:
      start_time = time.strftime('%Y-%m-%dT%H:%M:%S.000Z', time.gmtime(time.time() + 720))
    if cem.end_time is None:
      end_time = time.strftime('%Y-%m-%dT%H:%M:%S.000Z', time.gmtime(time.time() + 3600))

    event.when.append(gdata.calendar.When(start_time=start_time, end_time=end_time))
    event.when[0].reminder.append(reminder)

    new_event = calendar_service.InsertEvent(event, '/calendar/feeds/default/private/full')

#    print 'New single event inserted: %s' % (new_event.id.text,)
#    print '\tEvent edit URL: %s' % (new_event.GetEditLink().href,)
#    print '\tEvent HTML URL: %s' % (new_event.GetHtmlLink().href,)

    return new_event

def create_calendar_service(email='rasingbull@gmail.com', password='angry2000'):
    calendar_service = gdata.calendar.service.CalendarService()
    calendar_service.email = email
    calendar_service.password = password
    calendar_service.source = 'Google-Calendar_Python_Sample-1.0'
    calendar_service.ProgrammaticLogin()

    return calendar_service

def send_cem_by_sms(cem):
    calendar_service = create_calendar_service()
    event = create_event(calendar_service, cem)
#    import time
#    time.sleep(120)# delete after 1 min
#    calendar_service.DeleteEvent(event.GetEditLink().href)

if __name__ == '__main__':
    cem = CalendarEventMsg(
            title=u'오늘의환율',
            content=u'100원',
            )
    send_cem_by_sms(cem)